package com.jason.quectel0805.activities;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jason.quectel0805.R;
import com.jason.quectel0805.adapters.MyAdapter;
import com.jason.quectel0805.adapters.SelectAdapter;
import com.jason.quectel0805.entities.Select;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewItemSelectActivity extends AppCompatActivity {
    private RecyclerView rvShow;
    private SelectAdapter myAdapter;
    private List<Select> totalList = new ArrayList<>();
    private Button btnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_item_select);
        btnDelete = findViewById(R.id.btn_delete);
        rvShow = findViewById(R.id.rv_select_show);
        rvShow.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        //2.创建adapter适配器
        myAdapter = new SelectAdapter(totalList, this);
        //3、将adapter和recyclerview进行绑定
        rvShow.setAdapter(myAdapter);
        //    4、设置adapter的数据源
        mockData();

        initListener();
    }


    private void initListener() {
        //给Recyclerview设置点击监听,发现recyclerview并没有相关给item单击设置监听器的方法，只能自己手写监听器
        myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (myAdapter.isShowCheckBox()) {
                    Select select = totalList.get(position);
                    select.isSelected = !select.isSelected;
                }
                myAdapter.notifyDataSetChanged();
            }
        });
        //设置长按监听
        myAdapter.setOnItemLongClickListener(new MyAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int position) {
                for (Select select : totalList) {
                    select.isSelected = false;
                }
                myAdapter.setShowCheckBox(true);
                myAdapter.notifyDataSetChanged();
                btnDelete.setVisibility(View.VISIBLE);
                return true;
            }
        });
    }

    private void mockData() {
        for (int i = 0; i < 50; i++) {
            Select select = new Select("小米手机" + i, false);
            totalList.add(select);
        }
        myAdapter.notifyDataSetChanged();
    }

    /**
     * 删除已选择的数据
     *
     * @param view
     */
    public void deleteSelectedData(View view) {
        if (myAdapter.isShowCheckBox()) {
            showDialog();
        }
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        AlertDialog alertDialog = builder.setTitle("温馨提醒")
                .setMessage("您确认要删除吗？")
                .setPositiveButton("确认删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ArrayList<Select> notSelectList = new ArrayList<>();
                        //获取所有已选择的数据，然后删除
                        for (Select select : totalList) {
                            if (!select.isSelected) {
                                notSelectList.add(select);
                            }
                        }
                        totalList.clear();
                        totalList.addAll(notSelectList);
                        btnDelete.setVisibility(View.INVISIBLE);
                        myAdapter.setShowCheckBox(false);
                        myAdapter.notifyDataSetChanged();
                    }
                })
                .setNegativeButton("我再想想", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(RecyclerViewItemSelectActivity.this, "那你要好好想想哦", Toast.LENGTH_SHORT).show();
                    }
                }).create();
        alertDialog.show();
    }
}